<?php
class demoController extends JYController {
	public function indexAction() {
		// 加载用户登录信息
		if ($_SESSION['user']) {
			define('CSM_USER_ID', $_SESSION['user']['user_id']?$_SESSION['user']['user_id']:0);
			define('CSM_USER_NAME', $_SESSION['user']['username']?$_SESSION['user']['username']:0);
			define('CSM_UNIT_ID',$_SESSION['user']['unit_id']?$_SESSION['user']['unit_id']:0);
		
			if (!isset($_SESSION['monthaccess'])) {
			$_SESSION['monthaccess'] = JYVisit::getTodayVisit(CSM_USER_ID);
			$_SESSION['dayaccess'] = JYVisit::getMonthVisit(CSM_USER_ID);
			}
		} else {
			$this->alert('请登录后再查看.',"/");
			exit();
		}
		return $this->render();
	}
	
	public function callrunAction(){
		$visitSql = $_SESSION['visitZySql'];
		$fields = $visitSql['fields'];
		$where = $visitSql['where'];
		$page = $this->request->getGetParameter('p');
		$page = 1;
		$this->next = $page + 1;
		$sql = "SELECT {$fields} FROM ZHUYUAN ZY 
				LEFT JOIN PATIENT P ON ZY.REF_PATIENT_ID=P.PATIENT_ID
				LEFT JOIN LAST_VISIT LV ON 'ZY_'||ZY.ZY_NO||'_'||ZY.ZY_TIMES=LV.BIZ_CODE
				WHERE {$where}
				GROUP BY P.PATIENT_ID, P.TRUE_NAME, P.PHONE, P.BIRTH, P.SEX, ZY.RY_DATE, ZY.CY_DATE, ZY.CY_DEP, ZY.ZYDOC, ZY_AGE, ZY.BIZ_CODE, ZY.ZY_CODE, ZY.CY_DEP_NO, ZY.ZYDOC_NO,
				LV.LAST_VISIT_TIME, LV.VISIT_COUNT, LV.VISIT_RESULT, LV.VISIT_STATE
				ORDER BY ZY.ZY_CODE";
		
		$row = JYDB::query($sql, $page, 1);
		
		if (!$row) {
			return $this->alert('对不起，您现在已经回访到最后一位客户了！');
		}
		
		
		$pid = $row[0]['PATIENT_ID'];
		
		// 获取病人基本资料
		$this->pid = $pid;
		$this->patient = JYDB::find('PATIENT', "PATIENT_ID={$pid}");

		// 获取病人病人的最近住院信息
		$zhuyuan = JYDB::find('ZHUYUAN', "REF_PATIENT_ID={$pid}", '*', 'CY_DATE DESC');
		
		// 病人最近的手术信息
		if ($zhuyuan['IS_OPERATE']) {
			$this->operation = JYDB::findAll('OPERATION', "ZY_NO='{$zhuyuan['ZY_NO']}' AND ZY_TIMES='{$zhuyuan['ZY_TIMES']}'", '*', 'OP_TIMES DESC');
		}
		
		// 匹配本规则下的调查问卷
		
		$this->zhuyuan = $zhuyuan;
		return $this->render();	
	}
}